var gameArea = "";
var gameAreaWidth = "";
var gameAreaHeight = "";
var myAircraft = "";
var btn = "";
var shadow = "";

function gameBtn(){
    btn = document.querySelector(".gameBtn");
    btn.style.display = "none";
    shadow = document.querySelector(".shadow");
    shadow.style.display = "none";
    game.gameStart();

}

window.onload=function(){
    gameArea = document.querySelector(".gameArea");
    gameAreaWidth = gameArea.offsetWidth;
    gameAreaHeight = gameArea.offsetHeight;
    myAircraft = document.querySelector(".myAir");
    gameArea.onmousemove = function(e){
        if(game.status){
            myAirObj.fly(e.offsetX,e.offsetY);
        }
    }
    gameArea.onmousedown = function(e){
        if(game.status){
            myAirObj.sendBullet(e.offsetX,e.offsetY);
        }
    }

}

var myAirObj = {
    fly:function(X,Y){
        if(X > 53 && X < gameAreaWidth - 53){
            myAircraft.style.left = X - 53 + "px";
        }
        if(Y > 38 && Y < gameAreaHeight - 38){
            myAircraft.style.top = Y - 38 + "px";
        }
    },
    sendBullet:function(X,Y){
        if(X > 0 && X < (myAircraft.offsetWidth / 2)){
            this.left = (myAircraft.offsetWidth / 2);
        }else if(X > gameAreaWidth - (myAircraft.offsetWidth / 2) && X < (gameAreaWidth)){
            this.left = gameAreaWidth - (myAircraft.offsetWidth / 2);
        }else{
            this.left = X;
        }
        this.top = Y - (myAircraft.offsetHeight);
        game.bulletArray.push(new Bullet(this.left,this.top));
    }

}

var game={
    score:0,
    time:0,
    status:false,
    bulletArray:[],
    enemyAircraftArray:[],
    bulletFlyTime:0,
    enemyAircraftFlyTime:0,
    createEnemyAircraftTime:0,
    getTimeClock:0,
    flyBulletAndEnemyAircraft:function(){
        this.bulletFlyTime = setInterval(function(){
            for(var i=0;i<game.bulletArray.length;i++){
                game.bulletArray[i].fly();
            }
            game.isBomb();
        },10);
        this.enemyAircraftFlyTime = setInterval(function(){
            for(var i=0;i<game.enemyAircraftArray.length;i++){
                game.enemyAircraftArray[i].fly();
            }
        },30);
    },
    createEnemyAircraft:function(){
        this.createEnemyAircraftTime = setInterval(function(){
            game.enemyAircraftArray.push(new EnemyAircraft());
        },800);
    },
    isBomb:function(){
        for(let i=game.bulletArray.length - 1;i>=0;i--){
            var pos = game.bulletArray[i].isHit();
            if(pos != -1){
                game.bulletArray[i].disappear();
                game.enemyAircraftArray[pos].hit();
            }
        }
    },
    getScore:function(thisScore){
        this.score  += thisScore;
        var scoreNum = document.querySelector(".scoreNum");
        scoreNum.innerHTML = this.score;
        if(this.score > 500){
            scoreNum.innerHTML = 0;
            this.score = 0;
        }
        if(this.score < 0){
            scoreNum.innerHTML = 0;
            this.score = 0;
            game.gameOver();
        }
    },
    getTime:function(){
        var timeNum = document.querySelector(".timeNum");
        this.getTimeClock = setInterval(function(){
            timeNum.innerHTML = (++game.time);
        },1000);

    },
    gameStart:function(){
        this.status = true;
        this.score = 0;
        this.time = 0;
        this.getTime();
        this.flyBulletAndEnemyAircraft();
        this.createEnemyAircraft();
    },
    gameOver:function(){
        this.status = false;
        var scoreNum = document.querySelector(".scoreNum");
        scoreNum.innerHTML = 0;
        var timeNum = document.querySelector(".timeNum");
        timeNum.innerHTML = 0;


        clearInterval(this.bulletFlyTime);
        clearInterval(this.enemyAircraftFlyTime);
        clearInterval(this.createEnemyAircraftTime);
        clearInterval(this.getTimeClock);

        var rank = document.querySelector(".rank");
        if(this.time <= 30){
            rank.innerHTML = "倔强青铜";
        }else if(this.time <= 60){
            rank.innerHTML = "秩序白银";
        }else if(this.time <= 120){
            rank.innerHTML = "荣耀黄金";
        }else if(this.time <= 240){
            rank.innerHTML = "尊贵铂金";
        }else if(this.time <= 480){
            rank.innerHTML = "永恒钻石";
        }else{
            rank.innerHTML = "最强王者";
        }

        game.bulletArray = [];
        var bullet = document.getElementsByClassName("bullet");
        for(let i=0,len=bullet.length;i<len;i++){
            gameArea.removeChild(bullet[0]);
        }
        game.enemyAircraftArray = [];
        var enemyAircraft = document.getElementsByClassName("enemyAircraft");
        for(let i=0,len=enemyAircraft.length;i<len;i++){
            gameArea.removeChild(enemyAircraft[0]);
        }

        btn.style.display = "none";
        shadow.style.display = "flex";
       
        var flyTimeNum = document.querySelector(".flyTimeNum");
        flyTimeNum.innerHTML = this.time;
        var myCavas = document.querySelector(".myCavas");
        myCavas.style.display = "flex";
    }
}

var bulletId = 0;
function Bullet(X,Y){
    this.id = "bulelt" + (++bulletId) % 10000;
    this.left = X;
    this.top = Y;
    var thisBullet = document.createElement("div");
    thisBullet.id = this.id;
    thisBullet.className = "bullet";
    thisBullet.style.left = this.left + "px";
    thisBullet.style.top = this.top + "px";
    gameArea.appendChild(thisBullet);
}
Bullet.prototype.fly=function(){
    this.top -=5;
    var thisBullet = document.getElementById(this.id);
    if(this.top > 0){
        thisBullet.style.top = this.top + "px";
    }else{
        this.disappear();
    }
}
Bullet.prototype.disappear=function(){
    var thisBullet = document.getElementById(this.id);
    gameArea.removeChild(thisBullet);
    for(var i=0;i<game.bulletArray.length;i++){
        if(game.bulletArray[i] == this){
            game.bulletArray.splice(i,1);
        }
    }
}
Bullet.prototype.isHit=function(){
    for(let i=game.enemyAircraftArray.length - 1;i>=0;i--){
        var enemyAircraftWidth = document.getElementById(game.enemyAircraftArray[i].id).clientWidth;
        var enemyAircraftHeight = document.getElementById(game.enemyAircraftArray[i].id).clientHeight;
        if(Math.abs(this.left - game.enemyAircraftArray[i].left) < enemyAircraftWidth && Math.abs(this.top - game.enemyAircraftArray[i].top) < enemyAircraftHeight){
            return i;
        }
    }
    return -1;
}

var enemyAircraftId = 0;
function EnemyAircraft(){
    this.id = "enemyAircraft" + (++enemyAircraftId) % 10000;
    this.top = -76;
    this.score = 0;
    var changeColor = "";
    this.left = Math.random()*(gameAreaWidth - 98);
    switch(parseInt(Math.random()*3)){
        case 0:
            changeColor = "e1";
            this.score = 10;
            break;
            case 1:
                changeColor = "e2";
                this.score = 20;
                break;
                case 2:
                    changeColor = "e3";
                    this.score = 30;
                    break;  
    }
    var thisEnemyAircraft = document.createElement("div");
    thisEnemyAircraft.id = this.id;
    thisEnemyAircraft.className = "enemyAircraft";
    thisEnemyAircraft.classList.add(changeColor);
    thisEnemyAircraft.style.left = this.left + "px";
    thisEnemyAircraft.style.top = this.top + "px";
    gameArea.appendChild(thisEnemyAircraft);
}
EnemyAircraft.prototype.fly=function(){
    this.top +=1;
    var thisEnemyAircraft = document.getElementById(this.id);
    if(this.top <gameAreaHeight){
        thisEnemyAircraft.style.top = this.top + "px";
    }else{
        gameArea.removeChild(thisEnemyAircraft);
        game.getScore(this.score*(-5));
        for(var i=0;i<game.enemyAircraftArray.length;i++){
            if(game.enemyAircraftArray[i] == this){
                game.enemyAircraftArray.splice(i,1);
            }
        }
    }
}
EnemyAircraft.prototype.hit=function(){
    var thisEnemyAircraft = document.getElementById(this.id);
   thisEnemyAircraft.style.backgroundImage = "url('./img/boom.gif')";
   thisEnemyAircraft.style.backgroundSize = "cover";
   setTimeout(function(){
    gameArea.removeChild(thisEnemyAircraft);
   },500);
     for(var i=0;i<game.enemyAircraftArray.length;i++){
            if(game.enemyAircraftArray[i] == this){
                game.enemyAircraftArray.splice(i,1);
            }
        }
    game.getScore(this.score);
}


function reGameBtn(){
    gameBtn();
}











